package com.dehui.traders.ui.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.adapter.OptionCodeAdapter;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.bean.IndexInfo;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.config.Config;
import com.dehui.traders.databinding.FragmentHomeBinding;
import com.dehui.traders.kchart.MyCombinedChartX;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.widgets.HomeReView;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by hqq on 18.3.29.
 */

public class HomeFragment extends BaseFragment<FragmentHomeBinding,HomeViewModel> implements AdapterView.OnItemClickListener, HomeReView.OnRecyclerViewChangeListener, TableAdapter.OnTableItemClick {

    public static boolean isHomeVisity;
    private HomeDataAdapter homeAdapter;
    private MyCombinedChartX mChartPrice;
    private HomeViewModel viewModel;
    private List<IndexInfo> indexList = new ArrayList<>();
    private List<RealtimeQuote> optionList = new ArrayList<>();
    private OptionCodeAdapter adapter;
    public HorizontalScrollView mTouchView;
    private int clickPos = 0;
    private MyCombinedChartX mChartVolume;
    private IndexInfo mCurrentInfo;
    private View view;

    @Override
    public int initContentView() {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeViewModel initViewModel() {
        viewModel = new HomeViewModel(getContext(),this);
        return viewModel;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = super.onCreateView(inflater, container, savedInstanceState);
        }

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);

        return view;
    }

    @Override
    public void initData() {
        initChart();
        viewModel.initChartView(mChartPrice,mChartVolume);
        homeAdapter = new HomeDataAdapter(getContext(),indexList, R.layout.list_item_home_data,this);
        binding.rvHome.setAdapter(homeAdapter);
        binding.rvHome.setOnRecyclerViewChangeListener(this);
        initViews();
    }

    private void initViews(){
        adapter = new OptionCodeAdapter(getContext(), optionList, this);
        binding.tabView.setFixes(Config.indexTitle)
                .setOthers(Config.optionOtherTitle)
                .setFragment(this)
                .setOnItemClick(this)
                .setAdapter(adapter).build();
    }

    @Override
    public void onResume() {
        super.onResume();
        isHomeVisity = true;
        viewModel.refreshOptionData();
    }

    @Override
    public void onPause() {
        super.onPause();
        isHomeVisity = false;
    }

    private void initChart() {
        mChartPrice = binding.mycombinedchartKLine;
        mChartVolume = binding.mycombinedchartVolume;
        mChartPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StocksDetailActivity.startType(getActivity(),"首页",mCurrentInfo.name,mCurrentInfo.code,String.valueOf(mCurrentInfo.preclose),mCurrentInfo.market,1);
            }
        });
        mChartVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StocksDetailActivity.startType(getActivity(),"首页",mCurrentInfo.name,mCurrentInfo.code,String.valueOf(mCurrentInfo.preclose),mCurrentInfo.market,1);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        clickPos = position;
        mCurrentInfo = indexList.get(position);
        viewModel.getTodayTicks(mCurrentInfo.preclose,mCurrentInfo.market + mCurrentInfo.code,mCurrentInfo.name);
    }

    @Override
    public void visityPositionChange(int position) {
        homeAdapter.setSelectPos(position);
    }

    public void refreshRealTimeQuotesList(List<IndexInfo> indexList,boolean isCache) {
        this.indexList = indexList;
        homeAdapter.refresh(indexList);
        if (indexList != null || indexList.size() > clickPos && !isCache){
            mCurrentInfo = indexList.get(clickPos);
            viewModel.getTodayTicks(mCurrentInfo.preclose,mCurrentInfo.market + mCurrentInfo.code,mCurrentInfo.name);
        }
    }

    @Override
    public void onItemClick(int position) {
        RealtimeQuote realtimeQuote = optionList.get(position);
        StocksDetailActivity.start(getActivity(),"首页",realtimeQuote.name,
                realtimeQuote.code,String.valueOf(realtimeQuote.closePrice),viewModel.getMarket(position));
    }


    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        List<MyHScrollView> mHScrollViews = binding.tabView.getScrollViews();
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }

    public void updataList(List<RealtimeQuote> list) {
        optionList.clear();
        optionList.addAll(list);
        adapter.notifyDataSetChanged();
    }

}
